package org.indi.graduatemanagement.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.indi.graduatemanagement.common.dto.GradesDto;
import org.indi.graduatemanagement.common.dto.ScoreKeyValue;
import org.indi.graduatemanagement.common.lang.Result;
import org.indi.graduatemanagement.entity.Grades;
import org.indi.graduatemanagement.service.GradesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zcl
 * @since 2021-05-12
 */
@RestController
@RequestMapping("/grades")
public class GradesController {

    @Autowired
    GradesService gradesService;

//     = new GradesDto();

    @GetMapping("/{id}")
    @RequiresAuthentication //登录后可查询
    @RequiresPermissions("search")
    public Result getGrades(@PathVariable("id") String id, GradesDto gradesDto) {
        List<Grades> gradesList = gradesService.list(new QueryWrapper<Grades>().eq("graduates_ID", id));
        if (CollectionUtils.isEmpty(gradesList)) {
            return Result.fail("未找到该毕业生任何成绩");
        }
        gradesDto.setGraduatesId(id);
        for (Grades grades : gradesList) {
            ScoreKeyValue scoreKeyValue = new ScoreKeyValue();
            scoreKeyValue.setId(grades.getId());
            scoreKeyValue.setGradesKey(grades.getGradesKey());
            scoreKeyValue.setGradesValue(grades.getGradesValue());
            gradesDto.getGrades().add(scoreKeyValue);
        }
        return Result.successful(gradesDto);
    }

    @PostMapping("/save")
    @RequiresPermissions("add")
    @RequiresAuthentication //登录后可查询
    public Result saveGrades(@Validated @RequestBody Grades grades) {
        Grades gradesTemp = gradesService.getOne(new QueryWrapper<Grades>().eq("graduates_ID", grades.getGraduatesId()).eq("grades_key", grades.getGradesKey()));
        if (gradesTemp != null) {
            return Result.fail("该科目成绩已存在");
        }
        gradesService.save(grades);
        Grades return_grades = gradesService.getOne(new QueryWrapper<Grades>().eq("graduates_ID", grades.getGraduatesId()).eq("grades_key", grades.getGradesKey()));
        return Result.successful(return_grades);
    }

    @PostMapping("/update")
    @RequiresPermissions("update")
    @RequiresAuthentication //登录后可查询
    public Result updateGrades(@Validated @RequestBody ScoreKeyValue scoreKeyValue) {
        Grades grades = gradesService.getById(scoreKeyValue.getId());
        if (grades.getGradesKey().equals(scoreKeyValue.getGradesKey())) {
            grades.setGradesValue(scoreKeyValue.getGradesValue());
            gradesService.updateById(grades);
            return Result.successful("更新成功");
        }
        return Result.fail("科目不存在");
    }

    @RequiresPermissions("delete")
    @DeleteMapping("{id}")
    @RequiresAuthentication //登录后可查询
    public Result deleteGradesById(@PathVariable("id") Integer id) {
        if (gradesService.removeById(id)) {
            return Result.successful(null);
        } else {
            return Result.fail("删除失败");
        }
    }
}
